﻿#include"SectionDeleteDialog.h"
#include"ui_SectionDeleteDialog.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/PropertyWidget.h"
#include "OperatorsInterface/ParaWidgetInterfaceOperator.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFPhysicsData.h"

namespace GUI
{
    SectionDeleteDialog::SectionDeleteDialog(Interface::FITKAbstractSection * obj, EventOper::ParaWidgetInterfaceOperator * oper) :
        GUIDialogBase(FITKAPP->getGlobalData()->getMainWindow()), _oper(oper), _obj(obj)
    {
        _ui = new Ui::SectionDeleteDialog();
        _ui->setupUi(this);

        if (_obj) {
            QString text = tr("Delete %1").arg(_obj->getDataObjectName());
            _ui->label_Name->setText(text);
        }

        setWindowTitle(tr("Section Delete"));
    }

    SectionDeleteDialog::~SectionDeleteDialog()
    {
        if (_ui)delete _ui;
    }

    void SectionDeleteDialog::on_pushButton_OK_clicked()
    {
        if (_obj == nullptr)return;

        Interface::FITKOFPhysicsData* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Interface::FITKOFPhysicsData>();
        if (physicsData == nullptr)return ;
        Interface::FITKAbstractSectionManager* sectionManager =physicsData->getSectionManager();
        if (!sectionManager)return ;//获取失败返回nullpttr，因此要检查返回值是否为空，避免空指针

        //判断删除的数据是否是当前界面
        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        //所要删除的几何对象ID
        int deleteObjId = _obj->getDataObjectID();

        //清除几何对象
        sectionManager->removeDataByID(deleteObjId);
        _oper->execProfession();
        this->accept();
    }

    void SectionDeleteDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }
}


